import { UserLogin } from '@/api/user'
import BgMusicControl from '@/components/BgMusicControl'
import { initWXConfig } from '@/utils'
import { useEffect, useState } from 'react'

import { Outlet, useSearchParams } from 'umi'

export default function Layout() {
  const [openId, setOpenId] = useState<string | undefined>(
    localStorage.getItem('openId') || undefined,
  )

  useEffect(() => {
    initWXConfig() // 初始化微信分享。
    if (!openId) {
      UserLogin().then((userInfo) => {
        userInfo?.id && localStorage.setItem('userId', `${userInfo?.id}`)
        if (userInfo.openid) {
          setOpenId(userInfo.openid)
          localStorage.setItem('openId', userInfo?.openid)
        }
      })
    }
  }, [])

  if (!openId)
    return <p className="text-xs text-center">正在登录</p>

  return (
    <>
      <BgMusicControl />
      <Outlet />
    </>
  )
}
